package org.UserHouse.Servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.UserHouse.Dao.Imp.UserDaoImp;
import org.UserHouse.Dao.UserDao;
import org.UserHouse.entity.User;
import org.UserHouse.vo.PageVo;
import org.UserHouse.vo.ResponseData;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bin
 * @date 2024-11-28
 */
@WebServlet("/List.do")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String username = req.getParameter("uname") ;
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        User user = new User() ;
        user.setUname(username);

        int intPage = 1 ;
        if(page!=null && page.matches("\\d+")) {
            intPage = Integer.parseInt(page) ;
        }

        int intLimit = 5 ;
        if(limit!=null && limit.matches("\\d+")) {
            intLimit = Integer.parseInt(limit) ;
        }



        PrintWriter out = resp.getWriter();
        UserDao userDao = new UserDaoImp();
        List<User> users = userDao.selectPage(user,intPage,intLimit) ;
        Long count = userDao.selectPageCount(user);

        PageVo pageVo = new PageVo() ;

        pageVo.setCode(0);
        pageVo.setData(users);
        pageVo.setCount(count);


        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd:HH").create();
        String json = gson.toJson(pageVo);
        out.println(json);

        out.flush();
        out.close();
    }
}
